//
//  DFCamerPreView
//  自定义美颜
//
//  Created by zhao on 16/8/2.
//  Copyright © 2016年 zhao. All rights reserved.
//

#import "DFCamerPreView.h"
#import "UIView+DFExtension.h"

@interface DFCamerPreView ()

/**闪光灯按钮*/
@property (nonatomic, strong)  UIButton *flashButton;
/**前后摄像头切换按钮*/
@property (nonatomic, strong)  UIButton *turnButton;
/**相册按钮*/
@property (nonatomic, strong) UIButton *photoBroswBtn;
/**拍照按钮*/
@property (nonatomic, strong) UIButton *takePhotoButton;
/**取消按钮*/
@property (nonatomic, strong) UIButton *cancelButton;

@end

@implementation DFCamerPreView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpUI];
    }
    return self;
}
#pragma mark - setUpUI
- (void)setUpUI{
    self.setupView = [[UIView alloc]init];
    self.setupView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.setupView];
    
    self.flashButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.flashButton setTitle:@"⚡️自动" forState:UIControlStateNormal];
    [self.flashButton sizeToFit];
    [self.flashButton addTarget:self action:@selector(didClickChangeFlashMode:) forControlEvents:UIControlEventTouchUpInside];
    [self.setupView addSubview:self.flashButton];
    
    self.turnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.turnButton setTitle:@"切换" forState:UIControlStateNormal];
    [self.turnButton addTarget:self action:@selector(didClickTurnLight:) forControlEvents:UIControlEventTouchUpInside];
    [self.setupView addSubview:self.turnButton];
    
    self.photoBroswBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.photoBroswBtn addTarget:self action:@selector(didClickphotoBroswBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.setupView addSubview:self.photoBroswBtn];
    
    self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.takePhotoButton setImage:[UIImage imageNamed:@"photograph"] forState:UIControlStateNormal];
     [self.takePhotoButton setImage:[UIImage imageNamed:@"photograph_Select"] forState:UIControlStateHighlighted];
    [self.takePhotoButton addTarget:self action:@selector(didClickTakePhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.setupView addSubview:self.takePhotoButton];
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(didClickCancelButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.setupView addSubview:self.cancelButton];
}
#pragma mark - layoutSubviews
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.setupView.frame = self.bounds;
    NSLog(@"%@",NSStringFromCGRect(self.flashButton.frame));
    CGFloat btnW = 44;
    CGFloat btnH = btnW;
    CGFloat margin = 10;
//    self.flashButton.size = CGSizeMake(btnW, btnH);
//    self.flashButton.height = 44;
    self.flashButton.x = margin;
    self.flashButton.y = margin;
    
    self.turnButton.size = CGSizeMake(btnW, btnH);
    self.turnButton.x = self.width - margin - self.turnButton.width;
    self.turnButton.y = margin;
    
    self.photoBroswBtn.size = CGSizeMake(btnW, btnH);
    self.photoBroswBtn.x = margin;
    self.photoBroswBtn.y = self.height - margin -self.photoBroswBtn.height;
    self.photoBroswBtn.layer.cornerRadius = self.photoBroswBtn.width * 0.5;
    self.photoBroswBtn.layer.masksToBounds = YES;
    
    self.takePhotoButton.size = CGSizeMake(btnW, btnH);
    self.takePhotoButton.centerX = self.width * 0.5;
    self.takePhotoButton.y = self.height - margin -self.takePhotoButton.height;
    
    self.cancelButton.size = CGSizeMake(btnW, btnH);
    self.cancelButton.x = self.width - margin - self.cancelButton.width;
    self.cancelButton.y = self.height - margin - self.cancelButton.height;
    
}
#pragma mark - 按钮的点击事件
/**切换闪光灯*/
- (void)didClickChangeFlashMode:(UIButton *)button {

    if ([self.delegate respondsToSelector:@selector(didClickChangeFlashMode:)]) {
        [self.delegate didClickChangeFlashMode:button];
    }
}
/**切换前后摄像头*/
- (void)didClickTurnLight:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(didClickTurnLight:)]) {
        [self.delegate didClickTurnLight:button];
    }
}
/**点击相册预览*/
- (void)didClickphotoBroswBtn:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(didClickphotoBroswBtn:)]) {
        [self.delegate didClickphotoBroswBtn:button];
    }
}
/**点击拍照*/
- (void)didClickTakePhoto:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(didClickTakePhoto:)]) {
        [self.delegate didClickTakePhoto:button];
    }
}
/**点击取消*/
- (void)didClickCancelButton:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(didClickCancelButton:)]) {
        [self.delegate didClickCancelButton:button];
    }
}

-(void)setCurrentImage:(UIImage *)currentImage {

    if (currentImage) {
        [self.photoBroswBtn setImage:currentImage forState:UIControlStateNormal];
    }
}

@end
